package com.zingrow.web.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zingrow.common.utils.CommonUtils;
import com.zingrow.common.utils.MD5Util;
import com.zingrow.common.utils.PageView;
import com.zingrow.web.base.service.impl.BaseService;
import com.zingrow.web.configure.iformation.mapper.IformationMapper;
//import com.zingrow.web.configure.information.mapper.InformationMapper;
import com.zingrow.web.customer.model.GroupScheme;
import com.zingrow.web.report.mapper.RptEnshrineMapper;
import com.zingrow.web.user.mapper.UserMapper;
import com.zingrow.web.user.mapper.UserModelSchemeMapper;
import com.zingrow.web.user.mapper.UserPermissionMapper;
import com.zingrow.web.user.model.User;
import com.zingrow.web.user.model.UserPermission;
import com.zingrow.web.user.service.IUserService;

@Service
public class UserService extends BaseService<User> implements IUserService {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserPermissionMapper userPermissionMapper;
	@Autowired
	private IformationMapper iformationMapper;
	@Autowired
	private UserModelSchemeMapper userModelSchemeMapper;
	@Autowired
	private RptEnshrineMapper rptEnshrineMapper;
	

	@Override
	public void initMapper() {
		this.baseMapper = this.userMapper;
	}

	@Override
	public User checkUserLogin(String account, String password) {
		// 密码转换为md5加密后的字符串
		//String upassword = MD5Util.MD5Encode(password, null);

		Map<String, String> map = new HashMap<String, String>();
		map.put("account", account);
		map.put("userPassword", password);

		User user = userMapper.checkUserLogin(map);

		return user;
	}
	/**
	 * 根据参数查询    只查询APP用户和web用户
	 */
	@Override
	public List<User> queryByParams(PageView pageView, String account, Integer userType, String group,String name,String phone,String mailbox) {
		Map<String, Object> map = new HashMap<>();
		// 添加分页参数
		map.put("paging", pageView);
		// 添加所需要查询的参数
		if (StringUtils.isNotBlank(account)) {
			account = "%" + account + "%";
			map.put("account", account);
		}
		if (StringUtils.isNotBlank(name)) {
			name = "%" + name + "%";
			map.put("name", name);
		}
		if (StringUtils.isNotBlank(phone)) {
			phone = "%" + phone + "%";
			map.put("phone", phone);
		}
		if (StringUtils.isNotBlank(mailbox)) {
			mailbox = "%" + mailbox + "%";
			map.put("mailbox", mailbox);
		}
	
		
		map.put("userType", userType);
		map.put("group", group);
		map.put("name", name);
		map.put("phone", phone);
		map.put("mailbox", mailbox);

		List<User> list = userMapper.selectByParams(map);

		List<GroupScheme> inforList = new ArrayList<GroupScheme>();

		if (list == null || list.size() <= 0)
			return list;
		HashSet<String> set = new HashSet<String>();

			for (User user : list) {
				String ropeProgramme = user.getRopeProgramme();
				if (ropeProgramme == null || "".equals(ropeProgramme)) {
					continue;
				}
				String[] pro = ropeProgramme.split(",");
				for (String st : pro) {
					set.add(st);
				}
			}
			List<String> idList = new ArrayList<String>(set);
			if (idList.isEmpty() || idList == null)
				inforList = null;
			else				
				// 根据ID查找出方案名
				inforList = iformationMapper.queryByIDList(idList);
			User userAll = new User();
			userAll.setUserId(0);
			userAll.setIfmInfo(inforList);

			list.add(userAll);

		return list;
	}
	/**
	 * 根据参数查询    只查询超级管理员和普通管理用户
	 * @param pageView
	 * @param account
	 * @return
	 */
	@Override
	public List<User> queryParams(PageView pageView, String account){
		Map<String, Object> map = new HashMap<>();
		// 添加分页参数
		map.put("paging", pageView);
		// 添加所需要查询的参数
		if (StringUtils.isNotBlank(account)) {
			account = "%" + account + "%";
			map.put("account", account);
		}
		return userMapper.queryParams(map);
	}
	@Override
	public boolean querySameByUserName(String account, Integer userId, Integer userType) {
		return userMapper.selectSameByUserName(account, userId, userType) > 0;
	}
	@Override
	public boolean querySameByUserNameone(String name, Integer userId, Integer userType) {
		return userMapper.selectSameByUserNameone(name, userId, userType) > 0;
	}
	@Override
	public boolean querySameByUserPhone(String Phone, Integer userId, Integer userType) {
		return userMapper.selectSameByUserPhone(Phone, userId, userType) > 0;
	}
	@Override
	public boolean querySameByUserMailbox(String Mailbox, Integer userId, Integer userType) {
		return userMapper.selectSameByUserMailbox(Mailbox, userId, userType) > 0;
	}
     @Override
    public boolean addAdminUser(User user) {

    	return  userMapper.inserts(user)>0;
    }
     @Override
    public boolean updateUserNameAndKey(User user){
    	 return  userMapper.updateUserNameAndKey(user)>0;
     }
    @Override
    public Integer selectByUserAccount(String user){
    	
    	return userMapper.selectByUserAccount(user);
    }
	@Override
	public boolean addUser(User user) {
		if (user.getInsertTime() == null) {
			user.setInsertTime(new Date());
		}
		if (user.getIsDelete() == null) {
			user.setIsDelete(1);
		}
		if (user.getUserName() == null) {
			user.setUserName(user.getUserAccount());
		}
		if (user.getUserPwd() != null) {
			user.setUserPwd(MD5Util.MD5Encode(user.getUserPwd(), null));
		}
		
		int i = 0;

		if (userMapper.insert(user) > 0) {

			i = userPermissionMapper.addPro(user.getUserId());
		}

		return i > 0;
	}

	@Override
	@Transactional
	public boolean deleteUser(Integer userId) {

		int i = userMapper.deleteByPrimaryKey(userId);
		if (i > 0) {
			//删除用户，根据用户ID删除用户报告权限数据
			userModelSchemeMapper.delete(userId);
			//删除用户，根据用户ID删除用户收藏的报告
			rptEnshrineMapper.delEnShrineByUserId(userId);
			// 删除用户后，删除用户权限
			return userPermissionMapper.delByUserId(userId) > 0;
		}
		return false;
	}

	@Override
	public boolean updateByUserIDPro(Integer userId, String ropePro, String permission) {
		return userPermissionMapper.updateByUserIDPro(userId, ropePro, permission) > 0;
	}

	@Override
	public boolean updateByPrimaryKey(User user) {
		if (user.getUpdateTime() == null) {
			user.setUpdateTime(new Date());
		}
		if (CommonUtils.isStrNotNull(user.getUserPwd())) {
			user.setUserPwd(MD5Util.MD5Encode(user.getUserPwd(), null));
		}
		return userMapper.updateByPrimaryKey(user) > 0;
	}

	@Override
	public User queryByUserId(Integer userId) {

		return userMapper.selectByPrimaryKey(userId);
	}

	@Override
	public User checkUserName(String account) {
		return userMapper.checkUserName(account);
	}

	@Override
	public UserPermission selectByUserPer(Integer userId) {
		return userPermissionMapper.selectById(userId);
	}
	@Override
	public List<User> selectidName(String ropeProgramme){
		return userMapper.selectidName(ropeProgramme);
	}
	 @Override
    	public User queryGroupByUserName(String userAccount) {
        	return userMapper.queryGroupByUserName(userAccount);
    	}
	 /**
	  * 根据用户名查询用户信息id
	  */
	 @Override
	 public User selectName(String userAccount){
		 User user = userMapper.selectName(userAccount);
		 return user;
	 }
	 /**
	  * 根据用户id查询用户拥有哪些方案
	  * @param userId
	  * @return
	  */
	 @Override
	 public UserPermission selectByUserId(Integer userId){
		 return userPermissionMapper.selectByUserId(userId);
	 }
	 /**
	  * 根据组织id查询用户集合
	  * @param groupId
	  * @return
	  */
	 @Override
	 public List<User> selectId(Integer groupId){
		 return userMapper.selectId(groupId);
	 }
	 
	/*
	 * 修改用户方案
	 */
	@Override
	public boolean updateifmId(String ropeProgramme1, Integer userId) {
		return userPermissionMapper.updateifmId(ropeProgramme1, userId)>0;
	}
	
	/*
	 * 根据组织ID和方案查询
	 */
	@Override
	public List<UserPermission> selectuIdifmId(Integer groupId) {
		return userPermissionMapper.selectuIdifmId(groupId);
	}

	@Override
	public int selectByGroupId(Integer groupId) {
		return userMapper.selectByGroupId(groupId);
	}
	
	@Override
	public User querybyuserid(Integer userId) {
		return userMapper.querybyuserid(userId);
	}

}
